package com.hnas.common.util;

import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnas.api.dto.DTOResult;
import com.hnas.common.domain.DiscountType;
import com.hnas.core.util.DateUtil;
import com.hnas.tjhnaaw.orm.GenerateMrchTwoCode;
import com.hnas.trade.domain.DetailType;
import com.hnas.trade.dto.intf.CreateOrderDetailBean;

public class CommonUtils {
	
	public static ObjectMapper objectMapper = new ObjectMapper();
	
	public final static String NEED_QRCODE_TRADETYPE = "02";
	public final static String NEED_ACCOUNT_TRADETYPE = "01|02|03|04";
	public final static String SUPPORT_SUBCODE = "224101|224102|224103";

	
	public static String  getUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	public static String  getNewToken(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	@SuppressWarnings("finally")
	public static Date getTranTime(String tranDate, String tranTime){
		Date res = null;
		try{
			res = DateUtil.stringToDate(tranDate+tranTime, "yyyyMMddHHmmss");
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			return res;
		}
	}
	
	/**
	 * 创建Controller返回结果
	 * @param dtoResult 返回结果对象
	 * @param code 返回码
	 * @param result 返回的数据JSON字符串
	 * @return
	 */
	public static String createResult(DTOResult dtoResult,String code,String result){
		String res = "";
		try{
			dtoResult.setResultCode(code);
			dtoResult.setResultMsg(ResultCode.getResultValue(code));
			dtoResult.setResultData(result);
			res = objectMapper.writeValueAsString(dtoResult);
		}catch(Exception e1){
			e1.printStackTrace();
			dtoResult.setResultCode(ResultCode.FAIL);
			dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.FAIL));
			dtoResult.setResultData(result);
			try{
				res = objectMapper.writeValueAsString(dtoResult);
			}catch(Exception e2){
				e2.printStackTrace();
			}
		}
		return res;
	}
	
	public static String createResult(DTOResult dtoResult, String result){
		String res = "";
		try{
			dtoResult.setResultData(result);
			res = objectMapper.writeValueAsString(dtoResult);
		}catch(Exception e1){
			e1.printStackTrace();
			dtoResult.setResultCode(ResultCode.FAIL);
			dtoResult.setResultMsg(ResultCode.getResultValue(ResultCode.FAIL));
			dtoResult.setResultData(result);
			try{
				res = objectMapper.writeValueAsString(dtoResult);
			}catch(Exception e2){
				e2.printStackTrace();
			}
		}
		return res;
	}
	
	/**
	 * 当前时间累加秒数
	 * @param seconds
	 * @return
	 */
	public static String nowAddSeconds(Long seconds){
		Long result = (new Date().getTime()+seconds*1000);
		return DateUtil.dateTimeToString(new Date(result));
	}
	
	/**
	 * 比较折扣率，选择折扣率最小的
	 * @param vipRate
	 * @param mchRate
	 * @param empRate
	 * @return
	 */
	public static CreateOrderDetailBean getMinDiscouRate(Long needPay,Long mchRate,Long empRate,GenerateMrchTwoCode mch){
		String discountType = "";
		CreateOrderDetailBean bean = null;
		Long result = 100l;
		if(mchRate<=empRate){
			discountType = DetailType.MrchDiscount.getKey();
			result = mchRate;
		}else{
			discountType = DetailType.EmpDiscount.getKey();
			result = empRate;
		}
		if(100l==result){
			return null;
		}
		Long discountAmount = getDiscountAmount(needPay, result);
		if(discountAmount > 0){
			if(null!=mch){
				if(discountType.equals(DetailType.MrchDiscount.getKey())){
					bean = new CreateOrderDetailBean();
					bean.setType(DetailType.MrchDiscount.getKey());
					bean.setDiscountType(DiscountType.DISCOUNT);//打折
					bean.setRaletedId(mch.getMrchId());
					bean.setDiscountAmount(getDiscountAmount(needPay, result));
					bean.setProductName(mch.getMrchName());
					bean.setDiscountValue(result+"");
					return bean;
				}
			}
			if(discountType.equals(DetailType.EmpDiscount.getKey())){
				bean = new CreateOrderDetailBean();
				bean.setType(DetailType.EmpDiscount.getKey());
				bean.setDiscountType(DiscountType.DISCOUNT);//打折
				bean.setDiscountAmount(getDiscountAmount(needPay, result));
				bean.setProductName("海航员工打折");
				bean.setDiscountValue(result+"");
				return bean;
			}
			
		}
		
		return null;
	}
	
	/**
	 * 获取打折优惠金额
	 * @param needPay
	 * @param minConsumption
	 * @param discountRate
	 * @return
	 */
	public static Long getDiscountAmount(Long needPay,Long discountRate){
		Long discountAmount = 0l;
		//折扣率为0时候，折扣金额为0
		if(0l==discountRate){
			return discountAmount;
		}
		
		if(discountRate>=100l){
			return needPay;
		}
		
		double dis = needPay*((1-discountRate/100.0));
		//得到打折优惠的金额，并四舍五入
		discountAmount = getBigDecimal(dis);
		
		return discountAmount;
	}
	
	/**
	 * 四舍五入
	 * @param num
	 * @return
	 */
	public static Long getBigDecimal(Double num){
		BigDecimal bigDecimal = new BigDecimal(num).setScale(0, BigDecimal.ROUND_HALF_UP);
		return bigDecimal.longValue();
	}
	
	/**
	 * 是否是支持的科目号
	 * @param subCode
	 * @return
	 */
	public static boolean supportSubcode(String subCode){
		String[] subCodes = SUPPORT_SUBCODE.split("\\|");
		for(String str : subCodes){
			if(str.equals(subCode)) return true;
		}
		return false;
	}
	/**
	 * 是否是需要账户id的订单类型
	 * @param tradeType
	 * @return
	 */
	public static boolean isNeedAccount(String tradeType){
		String[] tradeTypes = NEED_ACCOUNT_TRADETYPE.split("\\|");
		for(String str : tradeTypes){
			if(str.equals(tradeType)) return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		System.out.println(nowAddSeconds(1200l));
		
	}
	
}
